
package state;

public class PawnStructureTable
{
    private long[] pawnHash;
    private long []whitePawns;
    private long []blackPawns;
    private int[] whiteScore;
    private int[] blackScore;
   
    public static long hits=0, stores=0, probes=0;
    
    /** The number of items in the hashtable  */
    private int capacity;

	public PawnStructureTable(int capacity)
	{
        this.capacity = capacity;
        this.pawnHash = new long[capacity];
        this.whiteScore = new int[capacity];
        this.blackScore = new int[capacity];
        this.whitePawns = new long[capacity];
        this.blackPawns = new long[capacity];
	}
	
	public void store(long pawnHash2,long whitePawns2, long blackPawns2, int whiteScore2, int blackScore2)
	{
		stores++;
        int index = index(pawnHash2);
        this.pawnHash[index] = pawnHash2;
        
        this.whiteScore[index] = whiteScore2;
        this.blackScore[index] = blackScore2;
        this.whitePawns[index] = whitePawns2;
        this.blackPawns[index] = blackPawns2;
	}

    /**
     * Whether or not the entry for the given state exists in the hashtable
     * @param hash  the 64-bit hash of the state
     * @return  <code>true</code> if a valid entry exists in the hashtable, <code>false</code> if not
     */
    public boolean exists(long hash, long whitePawns2, long blackPawns2)
    {
    	probes++;
    	int index2 = index(hash);
    	
        if ((pawnHash[index2]==hash) && ( whitePawns2==whitePawns[index2]) && ( blackPawns2==blackPawns[index2])) {
        	hits++;
        	return true;
        }
        	
        return false;
    }

    /**
     * Returns the score for the given state
     * @param hash  the 64-bit hash of the state
     * @return  the score in centipawns
     */
    public int[] getScore(long hash) {
    	 int[] retval = new int[2];
         retval[0] = whiteScore[index(hash)];
    	 retval[1] = blackScore[index(hash)];
    	 return retval;
    }
 
	/**
	 * Get the number of elements in the transposition table.
	 *
	 * @return	the size of the table
	 */
	public int size()
	{
		return this.capacity;
	}

    /**
     * Convert hash code into appropriate index for table by using the modulo function.
     * @param hash      the hash code of the game state
     * @return          the index in the table (array)
     */
    private int index(long hash)
    {
        return (int)(Math.abs(hash) % this.capacity);
    }
		
}


